#!/bin/bash
# -*- coding: utf-8 -*-
#
#  Uruk Update Manager
#  
#  Copyright 2022 hayder majid <hayder@riseup.net>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
NUM=0
NUMUPGRADE=${NUM}
PROCESS="/usr/share/urukUpdater/urukupdater.py"
TITL=$(echo -e "System Updating Info"; echo);
MESS1=$(echo -e "The apt is held by process!"; echo)
MESS2=$(echo -e "Opps! Something went wrong\!, \n please try again later"; echo)
#MESS3=$(echo -e "You have  $NUMUPGRADE  packages needed to be upgrade"; echo)
MESS4=$(echo -e "Your System is up to date!"; echo)
OKL=$(echo -e "Ok"; echo);
CHECK_PRO="$(ps -ef | grep '[p]ython3 /usr/share/urukUpdater/urukupdater.py' | awk '{print $2}')"


#from https://stackoverflow.com/questions/28195805/running-notify-send-as-root
NOTIFY() {
  #Detect the display server in use
  local disp_server="$(echo $XDG_SESSION_TYPE)"
  #Detect the name of the display in use
  if [ "${disp_server}" =  "wayland" ]; then
    dbus-launch notify-send --icon=/usr/share/urukUpdater/pixmap/urukupdater-held.svg "$@"
  else
    local display=":$(ls /tmp/.X11-unix/* | sed 's#/tmp/.X11-unix/X##' | head -n 1)"
    #Detect the user using such display
    local user=$(who | grep '('$display')' | awk '{print $1}' | head -n 1)
    #Detect the id of the user
    local uid=$(id -u $user)	
    sudo -u $user DISPLAY=$display DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus notify-send --icon=/usr/share/urukUpdater/pixmap/urukupdater.svg "$@"
  fi
}

if grep -q "It is held by process" "/usr/share/urukUpdater/status/err"; then
  rm -r /usr/share/urukUpdater/pixmap/logo/*;
  ln -s /usr/share/urukUpdater/pixmap/urukupdater-held.svg /usr/share/urukUpdater/pixmap/logo/logo.svg;
  NOTIFY   "Uruk Updater" "Another apt action is running!"
elif grep -q "Err:" "/usr/share/urukUpdater/status/err" || grep -q "E:" "/usr/share/urukUpdater/status/err" || grep -q "Err:" "/usr/share/urukUpdater/status/result"; then
  rm -r /usr/share/urukUpdater/pixmap/logo/*;
  ln -s /usr/share/urukUpdater/pixmap/urukupdater-error.svg /usr/share/urukUpdater/pixmap/logo/logo.svg;
  NOTIFY   "Uruk Updater" "System can't update now!"
elif [ "${NUMUPGRADE}" != 0 ] ; then
  rm -r /usr/share/urukUpdater/pixmap/logo/*;
  ln -s /usr/share/urukUpdater/pixmap/urukupdater-available.svg /usr/share/urukUpdater/pixmap/logo/logo.svg;
  NOTIFY  "Uruk Updater" "System update available!"
elif [ "${NUMUPGRADE}" = 0 ] ; then
  rm -r /usr/share/urukUpdater/pixmap/logo/*;
  ln -s /usr/share/urukUpdater/pixmap/urukupdater-up-to-date.svg /usr/share/urukUpdater/pixmap/logo/logo.svg;
fi
if [ "${CHECK_PRO}" != "" ]; then
    kill -9 $CHECK_PRO
    python3 /usr/share/urukUpdater/urukupdater.py &> /dev/null &
fi
exit 0
